package com.ajk.server.dao.provider;

import com.ajk.server.model.TestConfig;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ajk.server.model.query.TestConfigQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 自动评审结果-配置  <br>
 * t_test_config <br>
 * v1.0 2016-08-26
 * @see com.ajk.server.dao.TestConfigMapper
 */
 public class TestConfigProvider {
 
 	/**
 	* 新增 自动评审结果-配置
 	*/
 	public String insert(TestConfig domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert ignore into t_test_config(create_time,last_modify_time");
	 	if(domain.getCategoryId()!=null){
			sb.append(",category_id");
		}
	 	if(domain.getStartRate()!=null){
			sb.append(",start_rate");
		}
	 	if(domain.getEndRate()!=null){
			sb.append(",end_rate");
		}
	 	if(domain.getDoctorSuggest()!=null){
			sb.append(",doctor_suggest");
		}
	 	if(domain.getResultDesc()!=null){
			sb.append(",result_desc");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getCategoryId()!=null){
			sb.append(",#{categoryId}");
		}
	 	if(domain.getStartRate()!=null){
			sb.append(",#{startRate}");
		}
	 	if(domain.getEndRate()!=null){
			sb.append(",#{endRate}");
		}
	 	if(domain.getDoctorSuggest()!=null){
			sb.append(",#{doctorSuggest}");
		}
	 	if(domain.getResultDesc()!=null){
			sb.append(",#{resultDesc}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 自动评审结果-配置
 	*/
	public String update(TestConfig domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_test_config set last_modify_time = now() ");
		
		if(domain.getCategoryId()!=null){
			sb.append(",category_id=#{categoryId}");
		}
		if(domain.getStartRate()!=null){
			sb.append(",start_rate=#{startRate}");
		}
		if(domain.getEndRate()!=null){
			sb.append(",end_rate=#{endRate}");
		}
		if(domain.getDoctorSuggest()!=null){
			sb.append(",doctor_suggest=#{doctorSuggest}");
		}
		if(domain.getResultDesc()!=null){
			sb.append(",result_desc=#{resultDesc}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<TestConfig>> map){
		StringBuffer sb = new StringBuffer();
		List<TestConfig> testConfigs = map.get("list");
		sb.append("insert into t_test_config(create_time,last_modify_time,category_id,start_rate,end_rate,doctor_suggest,result_desc,in_used) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].categoryId}"
	    		+ ",#'{'list[{0}].startRate}"
	    		+ ",#'{'list[{0}].endRate}"
	    		+ ",#'{'list[{0}].doctorSuggest}"
	    		+ ",#'{'list[{0}].resultDesc}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(testConfigs)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  testConfigs.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 自动评审结果-配置
 	*/
	public String findByQuery(TestConfigQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_test_config a where 1>0 ");
		
		if(IntegerUtil.isPositiveInteger(query.getCategoryId())){
			sb.append(" and a.category_id = #{categoryId}");
		}
		sb.append(" order by a.end_rate desc");
		return sb.toString();
	}
	
	public String countByQuery(TestConfigQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_test_config a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 自动评审结果-配置
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_test_config where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1=2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
 	
 	public String findTestConfigByCategoryIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_test_config where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1=2");
		}else{
			if(ids.size()==1){
				sb.append(" category_id=").append("#{list[0]}");
			}else{
				sb.append(" category_id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	